package com.chanyachting.emall.action.yellowpage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Yellowpagesubcategory;
import com.chanyachting.emall.form.YellowpagesubcategoryForm;
import com.chanyachting.emall.util.RequestUtil;

public class YellowpagesubcategoryAction extends DispatchAction {
	
	
	public ActionForward listByCategory(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int cid=Integer.parseInt(ru.getParameter("cid", "0"));
		
		ru.setAttribute("categoryLit", Yellowpagesubcategory.listByCategoryId(cid));
		
		return mapping.findForward("list");
	}
	
	
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("categoryLit", Yellowpagesubcategory.list());
		
		
		return mapping.findForward("list");
	}
	
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		if(request.getMethod().equals("POST")){
			
			YellowpagesubcategoryForm subcategoryForm=(YellowpagesubcategoryForm)form;
			
			if(subcategoryForm.validate(mapping, request).isEmpty()){
				
				Yellowpagesubcategory subcategory=new Yellowpagesubcategory();
				
				BeanUtils.copyProperties(subcategory, subcategoryForm);
				
				if(subcategory.save()){
					
					ru.setSuccessMsg();
				}else{
					
					ru.setFailMsg();
				}
			}
		}
		
		ru.setAttribute("cid", ru.getParameter("categoryId", "0"));
	
		return mapping.findForward("create");
	}
	
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		if(request.getMethod().equals("POST")){
			
			YellowpagesubcategoryForm subcategoryForm=(YellowpagesubcategoryForm)form;
			
			if(subcategoryForm.validate(mapping, request).isEmpty()){
				
				Yellowpagesubcategory subcategory=new Yellowpagesubcategory(id);
				
				BeanUtils.copyProperties(subcategory, subcategoryForm);
				
				if(subcategory.update()){
					
					ru.setSuccessMsg();
				}else{
					
					ru.setFailMsg();
				}
			}
		}
		
		ru.setAttribute("subcategory", new Yellowpagesubcategory(id));
		
		return mapping.findForward("update");
	}
	
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Yellowpagesubcategory subcategory=new Yellowpagesubcategory(id);
		
		if(subcategory!=null){
			
			if(!subcategory.delete()){
				
				ru.setMsg("不能删除！请先删除该分类下的所有黄页关键字。");
			}
		}
		
		ru.setAttribute("cid", String.valueOf(subcategory.getCategoryId()));
		
		return mapping.findForward("delete");
	}
	
	

}
